//
//  MenuDarenViewController.m
//  Products_B
//
//  Created by lanou on 16/6/3.
//  Copyright © 2016年 朋学友. All rights reserved.
//


//  methodName=TalentRecommend&page=1&size=10&token=&user_id=&version=4.4
#import "MenuDarenViewController.h"
#import "MenuDarenModel.h"
#import "MenuDarenTableViewCell.h"
#import "MenuDarenZuopinViewController.h"
@interface MenuDarenViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableDictionary *parDic;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *modelArray;

@property (nonatomic, strong)NSString *page;
@end

@implementation MenuDarenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.modelArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 170;
    
    [self.tableView registerClass:[MenuDarenTableViewCell class] forCellReuseIdentifier:@"MenuDarenTableViewCell"];
    [self.view addSubview:_tableView];
    
    
   self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] + 1];
       self.parDic[@"page"] = self.page;
       [self requestData];
   }];
    
   self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      self.page = @"1";
       self.parDic[@"page"] = self.page;
       [self requestData];
   }];
    self.page = @"1";
    
    self.parDic = @{@"methodName":@"TalentRecommend",@"page":self.page,@"size":@"10",@"token":@"",@"user_id":@"",@"version":@"4.4"}.mutableCopy;
    [self requestData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuDarenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuDarenTableViewCell" forIndexPath:indexPath];
    MenuDarenModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    [cell.zanButton addTarget:self action:@selector(dianzanButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [cell.guanzhuButton addTarget:self action:@selector(guanzhuButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap1:)];
    cell.imageView1.userInteractionEnabled = YES;
    cell.imageView2.userInteractionEnabled = YES;
    cell.imageView3.userInteractionEnabled = YES;
    [cell.imageView1 addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap2:)];
    [cell.imageView2 addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap3:)];
    [cell.imageView3 addGestureRecognizer:tap3];
    return cell;
}
#pragma mark - 三个 imageView 的点击方法 -
- (void)imageViewTap1:(UITapGestureRecognizer *)tap{
    
    MenuDarenTableViewCell *cell = (MenuDarenTableViewCell *)tap.view.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    MenuDarenModel *model = self.modelArray[indexPath.row];
    MenuDarenZuopinViewController *zuopinVc = [[MenuDarenZuopinViewController alloc]init];
    zuopinVc.post_id = [model.array[0] id];
    [self.navigationController pushViewController:zuopinVc animated:YES];
}
- (void)imageViewTap2:(UITapGestureRecognizer *)tap{
    MenuDarenTableViewCell *cell = (MenuDarenTableViewCell *)tap.view.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    MenuDarenModel *model = self.modelArray[indexPath.row];
    MenuDarenZuopinViewController *zuopinVc = [[MenuDarenZuopinViewController alloc]init];
    zuopinVc.post_id = [model.array[1] id];
    [self.navigationController pushViewController:zuopinVc animated:YES];
}
- (void)imageViewTap3:(UITapGestureRecognizer *)tap{
    MenuDarenTableViewCell *cell = (MenuDarenTableViewCell *)tap.view.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    MenuDarenModel *model = self.modelArray[indexPath.row];
    MenuDarenZuopinViewController *zuopinVc = [[MenuDarenZuopinViewController alloc]init];
    zuopinVc.post_id = [model.array[2] id];
    [self.navigationController pushViewController:zuopinVc animated:YES];
}
#pragma mark - 点赞的BUtton
- (void)dianzanButtonAction:(UIButton *)button{
    
}
#pragma mark - 关注的BUtton
- (void)guanzhuButtonAction:(UIButton *)button{
    
}
- (void)requestData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
        if ([self.page isEqualToString: @"1"]) {
            [self.modelArray removeAllObjects];
        }
        
        NSArray *array = [MenuDarenModel modelConfigureWithDic:jsonDic];
        for (MenuDarenModel *model in array) {
            [self.modelArray addObject:model];
        }
        NSLog(@"%@",self.modelArray);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}


#pragma mark - 当push的时候隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
